<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>江西省财审管理系统</title>
<link rel="stylesheet" type="text/css" href="css/Login.css" />
<link rel="stylesheet" type="text/css" href="css/verifyCode.css" />
<link rel="stylesheet" type="text/css" href="/jxcsxm/js/autocomplete/jquery.autocomplete.css" />
<script type="text/javascript" src="./easyui/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="./easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="./js/util/jquery.cookie.js"></script>
<script type="text/javascript" src="./js/autocomplete/jquery.autocomplete.js" ></script>
<script type="text/javascript" src="./js/YMLib.js"></script>
<script type="text/javascript" src="./js/index.js"></script>
<script type="text/javascript" src="./js/base64.js"></script>
<script type="text/javascript" src="./js/verifyCode.js"></script>
<%--<script type="text/javascript" src="./js/jquery.md5.js"></script>--%>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/SM4.js">
    window.location.href = '/jxcsxm';
</script>
</head>
<body style="margin:0 0 0 0; min-width: 1000px;overflow-x:hidden; ">
<div id="bg">
	<div class="dlflash">
<div style="width:1000px;  height:258px;position:relative;top:49px;left:0px; margin:0 auto;">
<!-- <script type="text/javascript" src="js/wpFlashWrite.js"></script>
 --><script type="text/javascript">

/*  wpFlashWrite('images/login.png',1000,570,'wpFlash70814','','');
 */ var show_num;
 
 function randomNum(minNum, maxNum) {
	  switch (arguments.length) {
	    case 1:
	      return parseInt(Math.random() * minNum + 1, 10);
	      break;
	    case 2:
	      return parseInt(Math.random() * ( maxNum - minNum + 1 ) + minNum, 10);
	      //或者 Math.floor(Math.random()*( maxNum - minNum + 1 ) + minNum );
	      break;
	    default:
	      return 0;
	      break;
	  }
	} 
 
$(function(){
    if (window != top) {
        top.location.href = location.href;
    }

	$("#password").focus();
	$.ajax({
		 type : "POST",
		 url : "xtgl/getCode.do",
		 dataType : 'json',
		 ansync:false,
	     success : function(msg){
	    	 show_num=msg.toString().split("");
	    	 draw(show_num);
		}
	});
    $("#canvas").on('click',function(){
    	$.ajax({
   		 type : "POST",
   		 url : "xtgl/getCode.do",
   		 dataType : 'json',
   		 ansync:false,
   	     success : function(msg){
   	    	 show_num=msg.toString().split("");
   	    	 draw(show_num);
   		}
   	    });
    });

});
function rewrite(){
	$("#name").val("");
	$("#password").val("");
	$("#name").focus();
}
function autoCompletelogin(){
	$("#name").autocomplete("/jxcsxm/xtgl/userlogin.do", {
		multiple : false,
		minChars :2,
		multipleSeparator : ' ',
		mustMatch: true,
  		cacheLength : 0,
  		delay : 0,
  		max : 150,
  		extraParams : {
				name:function() {
  				var d = $("#name").val();
  				return d;
  			}
  		},
  		dataType : 'json',// 返回类型
  		// 对返回的json对象进行解析函数，函数返回一个数组
  		parse : function(data) {
  			var aa = [];
  			aa = $.map(eval(data), function(row) {
  					return {
  						data : row,
  						value : row.truename.replace(/(\s*$)/g,""),
  						result : row.truename.replace(/(\s*$)/g,"")
  					};
  				});
  			return aa;
  		},formatItem : function(row, i, max) {
  			return row.truename.replace(/(\s*$)/g,"");
  		}
  	}).result(function(e, item) {
  		//$("#name").val(item.truename)
	});
}
function checkText(){
	name = $("#name").val();
	password = $("#password").val();
	code = $("#code").val();
	if (code == ""){
		alert(' 验证码错误！');
		$("#code").focus();
		return;
	}if(name == ""){
		alert(' 用户名不能为空！');
		$("#name").focus();
		return;
	}else if(password == ""){
		alert(' 密码不能为空！');
		$("#password").focus();
		return;
	}
	bl = true;
}

function  SM4Encode(str) {
    return SM4.encode({input:str,key:'abcdefzhanxyygf'});
}

// 较验密码的合法性
function findPasswordValidate(str){
    var reg = new RegExp(/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&amp;*])[\da-zA-Z~!@#$%^&amp;*]{8,}$/);
    console.log('validate = ' + reg.test(str));
    if (reg.test(str)) {
        console.log('胜利通过验证！');
        return true;
    }
    console.log('没有通过验证！');
    return false;
}

function encryptionLogin(){
    checkText(); //检查文本框是否输入
    if (bl) {
        $.ajax({
            type : "POST",
            url : "xtgl/isLoginByEncryption.do",
            dataType : 'json',
            data : "master.truename="+name,
            success : function (msg) {
                console.log('msg = ' + msg);
                if(msg){
                    loginSM4();
                } else {
                    login();
                }
            },
            error : function (err) {
                console.log(err);
                YMLib.Tools.Show('服务器请求无响应！error code = 404',3000);
            }
        });
    }
}

function loginSM4() {
    // checkText(); //检查文本框是否输入
    var sm4Password = SM4Encode(password);
    if (bl) {
        $.ajax({
            type : "POST",
            url : "xtgl/enterLogin.do",
            dataType : 'json',
            data :"master.truename="+name+"&master.password="+encodeURIComponent(sm4Password)+"&master.code="+code,
            success: function (msg) {
                if(msg){
                    if(msg.NAME!=null) {
                        $.cookie("name",msg.NAME, {expires: 1});//将用户名放入cookie中
                    } else {
                        $.cookie("name","", {expires: 1});//将用户名放入cookie中
                    }
                    $.cookie("truename",msg.TRUENAME, {expires: 1});//将用户名放入cookie中

                    $.cookie("unit",msg.UNIT, {expires: 1});
                    var unit2=msg.UNIT;
                    if(unit2.substr(unit2.length-2,unit2.length)=="00") {
                        unit2=unit2.substr(0,unit2.length-2);
                    }
                    if(unit2.substr(unit2.length-2,unit2.length)=="00") {
                        unit2=unit2.substr(0,unit2.length-2);
                    }
                    if(msg.UNIT=="36") {
                        $.cookie("unit2","_____36", {expires: 1});
                    } else {
                        $.cookie("unit2",unit2, {expires: 1});
                    }
                    if(msg.UNIT=="36") {
                        $.cookie("dist","360000", {expires: 1});
                    } else {
                        $.cookie("dist",msg.UNIT.substr(msg.UNIT.length-6,msg.UNIT.length), {expires: 1});
                    }
                    var dist2=msg.UNIT.substr(msg.UNIT.length-6,msg.UNIT.length);
                    if(dist2.substr(dist2.length-2,dist2.length)=="00") {
                        dist2=dist2.substr(0,dist2.length-2);
                    }
                    if(dist2.substr(dist2.length-2,dist2.length)=="00") {
                        dist2=dist2.substr(0,dist2.length-2);
                    }
                    $.cookie("dist2",dist2, {expires: 1});
                    $.cookie("zgx",msg.ZGX, {expires: 1});

                    $.cookie("roleid",msg.ROLEID, {expires: 1});
                    selQxByUser1(msg.ROLEID);
                    $.cookie("cgdl","true", {expires: 1});
                } else{
                    alert("用户名、密码或验证码不正确！！");
                    $.ajax({
                        type : "POST",
                        url : "xtgl/getCode.do",
                        dataType : 'json',
                        ansync:false,
                        success : function(msg){
                            show_num=msg.toString().split("");
                            draw(show_num);
                        }
                    });
                }
            },
            error : function(){
                YMLib.Tools.Show('服务器请求无响应！error code = 404',3000);
            }
        });
    }
}

//点击登录按钮时
function login(){
	// checkText();//检查文本框是否输入
	var base64 = new Base64();
	var length=password.length; // csxt@1234
	if(length==10){length="A";}
	else if(length==11){length="B";}
	else if(length==12){length="C";}
	else if(length==13){length="D";}
	else if(length==14){length="E";}
	else if(length==15){length="F";}
	else if(length==16){length="G";}

	password =length+password+randomNum(10000000,99999999);
	password =base64.encode(password);
	if(bl){
		$.ajax({
			type : "POST",
			url : "xtgl/login.do",
			dataType : 'json',
			data :"master.truename="+name+"&master.password="+password+"&master.code="+code,
			success : function(msg){
				if(msg){
		     		if(msg.NAME!=null) {
                        //将用户名放入cookie中
                        $.cookie("name",msg.NAME, {expires: 1});
                    }
		     		else {
                        //将用户名放入cookie中
                        $.cookie("name","", {expires: 1});
                    }
					$.cookie("truename",msg.TRUENAME, {expires: 1});//将用户名放入cookie中
		     		$.cookie('token', encodeURIComponent(msg.PASSWORD), {expires: 1});
		     		$.cookie("unit",msg.UNIT, {expires: 1});
		     		var unit2=msg.UNIT;
		     		if(unit2.substr(unit2.length-2,unit2.length)=="00") unit2=unit2.substr(0,unit2.length-2);
		     		if(unit2.substr(unit2.length-2,unit2.length)=="00") unit2=unit2.substr(0,unit2.length-2);
		     		if(msg.UNIT=="36") $.cookie("unit2","_____36", {expires: 1});
		     			else $.cookie("unit2",unit2, {expires: 1});
		     		
		     		if(msg.UNIT=="36") $.cookie("dist","360000", {expires: 1});
		     		else $.cookie("dist",msg.UNIT.substr(msg.UNIT.length-6,msg.UNIT.length), {expires: 1});
		     		
		     		var dist2=msg.UNIT.substr(msg.UNIT.length-6,msg.UNIT.length);
		     		if(dist2.substr(dist2.length-2,dist2.length)=="00") dist2=dist2.substr(0,dist2.length-2);
		     		if(dist2.substr(dist2.length-2,dist2.length)=="00") dist2=dist2.substr(0,dist2.length-2);		     		
		     		$.cookie("dist2",dist2, {expires: 1});
		     		$.cookie("zgx",msg.ZGX, {expires: 1});
		     		
		     		$.cookie("roleid",msg.ROLEID, {expires: 1});
                    updateQxByUser(msg.ROLEID);
		     		// selQxByUser1(msg.ROLEID);
		     		$.cookie("cgdl","true", {expires: 1});
		     		
		     		
		     		//$('#index_layout').css('visibility', 'visible');
		     	 }
		     	 else{
		     		alert("用户名、密码或验证码不正确！！");
		     		$.ajax({
		     			 type : "POST",
		     			 url : "xtgl/getCode.do",
		     			 dataType : 'json',
		     			 ansync:false,
		     		     success : function(msg){
		     		    	 show_num=msg.toString().split("");
		     		    	 draw(show_num);
		     			}
		     		});
		     		
		     	 }
			 },
			 error : function(){
				 YMLib.Tools.Show('服务器请求无响应！error code = 404',3000);
			 }
		});
	}
	
}

</script>

                
               <div id="perDword" class="perDword" >
            	<table width="100%" border="0" cellspacing="0" cellpadding="0" height="128px">
           <tr>
           <td colspan="3">用户名：<input name="tbUser" type="text" id="name" style="height:23px;width:150px; line-height:23px;" /></td>
           </tr>
           <tr>
           <td colspan="3">密&nbsp;&nbsp;&nbsp;&nbsp;码：<input name="tbPwd" type="password" id="password" style="height:23px;width:150px; line-height:23px;" /></td>
           </tr>

            <tr>
                <td colspan="2">验证码：<input name="code" type="text" id="code" maxlength="4" style="height:23px;width:70px;line-height:23px;" />
                </td>
                <td align="left">
                    <canvas id="canvas" width="80" height="23" style="line-height:23px;position:relative;right:0px"></canvas><%----%>
                </td>
            </tr>

           <tr>
           <td width="15%"></td>
           <td align="center" > <input type="image" name="imgLogin" id="imgLogin"  src="images/dlClick.gif" onclick="encryptionLogin();" style="border-width:0px;" /></td>

           <td align="left">
           <input type="image" name="imgLogin" id="imgLogin"  src="images/DR2.gif" onclick="rewrite()" style="border-width:0px;" />
           </td>
           </tr>
           </table>
               </div> 
		</div>

</div>
</div>
</body>
</html>
